import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib

matplotlib.rc("font", family='Source Han Sans CN', weight="regular", size="10")

file_path = "csv/911.csv"
df = pd.read_csv(file_path)

title_series = df["title"].str.split(":")
title_list = [i[0] for i in title_series]
month_list = df["timeStamp"].str.slice(0, 7).tolist()

# 使用新增列方法与分组聚合统计数量
df["category"] = np.array(title_list).reshape((df.shape[0], 1))
df["months"] = np.array(month_list).reshape((df.shape[0], 1))
month_count = df.groupby(by=["category", "months"])["title"].count()

plt.figure(figsize=(20, 8), dpi=80)

plt.xlabel("时间")
plt.ylabel("数量")
plt.title("911电话报警类型随月份变化统计图", weight="bold", size="16")
plt.xticks(range(len(month_count["EMS"].index)), month_count["EMS"].index, rotation=45)

plt.plot(month_count["EMS"].index, month_count["EMS"].values, label="EMS")
plt.plot(month_count["Fire"].index, month_count["Fire"].values, label="Fire")
plt.plot(month_count["Traffic"].index, month_count["Traffic"].values, label="Traffic")
plt.legend()
plt.show()
